home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 21 / Mac Magazin and MacEasy Magazine CD - Issue 21.iso / Wissenschaft & Technik / yorick_docs folder / yorick_docs / FILE_FORMATS next >
Text File  |  1996-02-28  |  91KB  |  1,898 lines

  1.  
  2.            Binary Data File Formats and Descriptions
  3.            -----------------------------------------
  4.  
  5. ------------------------------------------------------------------------------
  6.  
  7.  
  8.  
  9. 1. Introducing the netCDF and PDB formats
  10. -----------------------------------------
  11.  
  12.    Scientific computing now takes place in a network of high
  13. performance UNIX workstations and mainframes.  Many sites include
  14. machines from several manufacturers on a single network.  In such a
  15. world it is crucial that programs be portable -- that is, that a
  16. program be written in a language and in a style which enables it to be
  17. compiled and run on as wide a variety of machines as possible.  It is
  18. just as crucial that the results of a portable program be written in a
  19. portable form -- that is, in a form legible by any machine on the
  20. network.  In order to save space and run fast, results of scientific
  21. calculations are written to disk in a binary format.  Unlike text
  22. files, binary files are not guaranteed to make sense when read back by
  23. a machine other than the machine on which they were written.  This is
  24. because the binary format used to represent numbers varies from one
  25. computer manufacturer to the next.
  26.  
  27.    Several solutions to this problem have emerged in the past few
  28. years.  Two existing solutions -- netCDF files and PDB files -- will
  29. be described in detail here.  A third, HDF vertex set format, is not
  30. too different in kind, and will not be discussed.  After a thorough
  31. examination of the strengths and weaknesses of these two formats, a
  32. "data language" is described which is capable of describing a very
  33. general binary data file -- including any netCDF or PDB file as a
  34. special case.
  35.  
  36.    The netCDF format is simple and widely used, but its authors
  37. (Unidata, sponsored by NSF) do not describe the actual disk format of
  38. the data in the documentation that comes with the software.  This is a
  39. peculiar omission, since no data format can be regarded as truly
  40. portable without being fully documented.  Furthermore, anyone can see
  41. that a heavily used data format cannot be changed in an incompatible
  42. way in the future -- any changes must take the form of additional
  43. structural information which does not conflict with the existing file
  44. format.  As you will see, the netCDF format is easily flexible enough
  45. to allow such additions without affecting the basic intelligibility of
  46. a netCDF file.
  47.  
  48.    The PDB file format is substantially more complicated and
  49. ambitious.  It has been heavily used, but only at the Lawrence
  50. Livermore National Laboratory, where it was designed by Stewart Brown.
  51. PDB is a drastically smaller programming effort than netCDF, and it
  52. has not been cleaned up to remove the evidence of its evolution.  Like
  53. netCDF, the underlying format of a PDB file is not described in the
  54. documentation for the package.  Despite the historical imperfections
  55. remaining in the PDB format, its full disclosure is just as important
  56. as full disclosure of the netCDF format.
  57.  
  58.    The documentation for both netCDF and PDB concentrates on the
  59. programming interface for reading and writing the files.  This is
  60. certainly understandable, but any file format could be accessed by
  61. many interfaces, and any interface could be realized using many file
  62. formats.  The file format is the data; the interface merely represents
  63. the data.  Only a complete description of the binary file format
  64. actually used by netCDF and PDB files makes any discussions of the
  65. merits and demerits of either format intelligible.
  66.  
  67.    In brief, a netCDF file consists of a small descriptive header,
  68. followed by the binary data described in the header.  Both the header
  69. and the data are written to disk using the XDR (eXternal Data
  70. Representation) library invented by Sun.  The XDR library converts
  71. integer and floating point numbers of various sizes into the IEEE
  72. standard floating point representation and big-endian order favored by
  73. Sun and many other computer manufacturers.  Each variable is of type
  74. byte, char (same as byte), short (2-byte integer), long (4-byte
  75. integer), float (4-byte real), or double (8-byte real), each may be an
  76. array of zero or more dimensions, and each may have any number of
  77. named attributes with associated attribute values.  The variables may
  78. be divided into a "non-record" group, and a "record" group.  "Record"
  79. variables are physically grouped at the end of the data section, and
  80. the layout of this subsection may be repeated an "unlimited" number of
  81. times to capture time-varying data.
  82.  
  83.    Similarly, a PDB file begins with a very small header describing
  84. the primitive data formats and specifiying the address of a longer
  85. descriptive section at the end of the file.  The binary data itself
  86. follows the small header.  Next comes a section in which any compound
  87. data types are named and defined in terms of the primitive data types
  88. (char, short, int, long, float, double, and pointered data).
  89. Next comes a symbol table, where a variable name, type, and dimension
  90. information are associated with a disk address in the data section.
  91. Finally, a special section allows for corrections to and extensions of
  92. the format not envisioned when the other descriptive components of the
  93. format were designed.  (Effectively, the PDB format has been a research
  94. tool for studying various notions about portable binary files.)
  95.  
  96.    The biggest difference between the netCDF and PDB strategies for
  97. data-portability is their handling of the primitive data formats.  The
  98. netCDF strategy is to mandate the exact representation of numbers
  99. within the file.  The PDB strategy is to describe the primitive data
  100. formats themselves, using a parameterization which is general enough
  101. to cover all currently interesting machines.  Both strategies have
  102. strengths and weaknesses.
  103.  
  104.    However, there are two advantages to the PDB strategy which deserve
  105. special mention: First, since PDB files can use the number
  106. representations native to any machine, they can be written and read at
  107. a similar speed on any platform; there is no advantage to owning a
  108. machine with number representations matching those of a Sun.  Second,
  109. PDB files can always represent a floating point number exactly (to the
  110. last bit) as long as they are read on a machine with the same number
  111. representations as the one on which they were written.  For a netCDF
  112. file, it is in principle possible that floating point data read back
  113. in will differ slightly from what was written out, owing to a
  114. round-trip conversion through XDR format.  In practice, this is rarely
  115. a problem, at least for the number representations in common use in
  116. the current generation of machines.
  117.  
  118.    Other differences arise because of the fact that the netCDF symbol
  119. table comes before the data, while the PDB symbol table comes
  120. afterward.  The conflict here is between wanting to be able to add
  121. variables to the file after some of the variable data has already been
  122. written (PDB can, netCDF can't), and robustness in the sense of always
  123. having the data description present in case the code or machine dies
  124. after some, but not all, of the data has been written (netCDF is
  125. robust, PDB isn't).
  126.  
  127.    Finally, there is a considerable difference between the kinds of
  128. objects easily described using a netCDF as opposed to a PDB format.
  129. To a certain degree, this is really a matter of the programming
  130. interface, which can obviously be built in such a way as to add a
  131. level of abstraction not explicitly represented in the underlying file
  132. format.  Thus, netCDF has "attributes" associated with each variable,
  133. and makes a simple provision for handling time-varying data.
  134. Conversely, PDB allows for compound data types (like C structs), and a
  135. simple disk representation of pointers.
  136.  
  137.    Despite all of these differences, one is struck by the common
  138. simple idea underlying both the netCDF and PDB file formats: The
  139. section of the file containing the binary data itself and the section
  140. which describes the binary data are completely separate.  There is no
  141. formatting information mixed into the binary data.  A second common
  142. design feature is that the symbol table information associates a
  143. variable name, type, and dimensions with the disk address where that
  144. variable begins.  Because of this commonality, it is easy to use the
  145. PDB machinery to read netCDF files, or the netCDF machinery to read
  146. PDB files written with Sun-like primitive data formats, simply by
  147. providing an alternate routine to open the file and create the data
  148. structure the rest of the package uses to describe the file
  149. internally.
  150.  
  151.    The common features of netCDF and PDB, as well as their respective
  152. strong points, motivate the design of the generic binary data
  153. description language described in the last section of this report.
  154. This language is christened "Clog" for Contents Log.  A generic
  155. programming interface capable of reading either netCDF or PDB files
  156. can be based on Clog.  Moreover, the data description language can
  157. make it possible to process the data in a completely arbitrary binary
  158. file with a single programming interface.
  159.  
  160.    A portable scientific program which both reads its input from and
  161. writes its output to data-portable self-descriptive binary files,
  162. allows for the most efficient use of a heterogeneous network of high
  163. performance computing engines.  The netCDF and PDB file formats are
  164. both suitable choices for the required binary I/O files, although each
  165. has definite strengths and weaknesses.  A general binary data
  166. description language is capable of describing either netCDF or PDB
  167. files, and can draw on the strengths of each paradigm.
  168.  
  169. ------------------------------------------------------------------------------
  170.  
  171.  
  172.  
  173. 2. The netCDF file format
  174. -------------------------
  175.  
  176.    A netCDF file consists of a shallow hierarchy of data types based on
  177. the primitive data types defined by the XDR standard.  This standard is
  178. fully described in the documents released by Sun Microsystems:
  179.  
  180.      XDR: eXternal Data Representation Standard, RFC1014
  181.      eXternal Data Representation: Sun Technical Notes
  182.      XDR(3N) UNIX man page
  183.  
  184.        available on Internet by anonymous FTP to ftp.uu.net:
  185.          /packages/bsd-sources/lib/librpc/doc/xdr.rfc.ms.Z
  186.          /packages/bsd-sources/lib/librpc/doc/xdr.nts.ms.Z
  187.          /packages/bsd-sources/lib/librpc/man/man3/xdr.3n.Z
  188.        the man page is available online on many UNIX systems
  189.  
  190. The netCDF software itself is available on Internet by anonymous FTP to
  191. unidata.ucar.edu, in the file /pub/netcdf/netcdf.tar.Z.  This includes
  192. complete documentation of the programming interface provided by Unidata
  193. to write and read netCDF files.
  194.  
  195.    The primitive data types used in a netCDF file are:
  196.  
  197.      opaque      - any number of bytes, padded with 0's to a multiple of 4
  198.      shorts      - any number of 2-byte integers in big-endian order,
  199.                    padded with 0's to a multiple of 4 bytes
  200.                    (implemented using the XDR_PUTBYTES and XDR_GETBYTES
  201.                     macros, 4 bytes at a time -- would have been cleaner
  202.                     and equivalent to implement using 4-byte opaque)
  203.      int, enum   - one 4-byte integer in big-endian order
  204.      long        - one signed 4-byte integer in big-endian order
  205.      float       - one 4-byte IEEE floating point number in big-endian order
  206.      double      - one 8-byte IEEE floating point number in big-endian order
  207.      u_long      - one unsigned 4-byte integer in big-endian order
  208.  
  209.    The netCDF file itself is at the opposite end of the type hierarchy,
  210. with the following intermediate layers:
  211.  
  212.      NC_array    - a counted list of objects of any other type
  213.                    The objects in an NC_array may be of variable length,
  214.                    so total length of an NC_array must be calculated by
  215.                    summing the lengths of its elements.
  216.      NC_var      - describes a variable in the file
  217.                    Each variable has a name, a data type (one of byte,
  218.                    char, short, long, float, or double), zero or more
  219.                    dimensions, zero or more attributes, and a disk
  220.                    address.
  221.      NC_dim      - Dimensions in a netCDF file are all named and shared
  222.                    among all variables in the file.  Each dimension has
  223.                    a name and a length.
  224.      NC_attr     - Each attribute has a name and a value; the value can
  225.                    be zero or more objects of any of the primitive data
  226.                    types (byte, char, short, long, float, or double).
  227.                    Attributes can belong to one variable, or to the
  228.                    file as a whole.
  229.  
  230.      NC_iarray   - a u_long count followed by that many ints
  231.      NC_string   - a u_long count followed by that many chars, written as
  232.                    an opaque
  233.  
  234.  
  235.  2A. Whole-file format
  236.  -----
  237.  
  238.    The entire netCDF file has the following data structure:
  239.  
  240.      u_long            0x43444601   "CDF\001", netCDF file magic number
  241.      u_long            numrecs      number of records
  242.      NC_array NC_dim   dims         name<-->dimension length associations
  243.      NC_array NC_attr  attrs        global attributes for this file
  244.      NC_array NC_var   vars         description of variables in this file
  245.      <any>             data         the variables described by vars
  246.  
  247. Here, the first column gives the type of the data identified in the
  248. second column, and described in the third column.  Each object up to
  249. the data section is written immediately after the preceding item; to
  250. make sense of this part of the file, it must be read back sequentially
  251. -- that is, first the dims, then the attrs, then the vars.  The vars,
  252. however, contain the disk addresses of all the variables in the data
  253. section, so the data in the file can be randomly accessed after vars
  254. has been read.
  255.  
  256.    Note that additional descriptive information could be added after
  257. vars without affecting the ability of the netCDF software to read the
  258. file.
  259.  
  260.  
  261.  2B. NC_array format
  262.  -----
  263.  
  264.      enum              type         0 unspecified
  265.                                     1 byte
  266.                                     2 char
  267.                                     3 short
  268.                                     4 long
  269.                                     5 float
  270.                                     6 double
  271.                                     7 bitfield (private)
  272.                                     8 string (private, NC_string)
  273.                                     9 iarray (private, NC_iarray)
  274.                                    10 dimension (private, NC_dim)
  275.                                    11 variable (private, NC_var)
  276.                                    12 attribute (private, NC_attr)
  277.  
  278.      u_long            count        number of objects in array
  279.      <any>             objects      byte, char written as xdr_opaque
  280.                                                of count bytes
  281.                                     short written as XDR_PUTBYTES
  282.                                           of count shorts (byte pairs)
  283.                                     all others written as a sequence
  284.                                           of count objects
  285.  
  286.    Note that the length of an NC_array is 8 bytes plus the aggregate
  287. length of the array elements.
  288.  
  289.  
  290.  2C. NC_var format
  291.  -----
  292.  
  293.      NC_string         name         name of the variable
  294.      NC_iarray         assoc        list of 0-origin indices into the
  295.                                     array of dimensions (dims) for this
  296.                                     file
  297.                                     The dimensions in the list are listed
  298.                                     slowest varying first.  If the slowest
  299.                                     dimension is the UNLIMITED dimension,
  300.                                     this is a record variable.
  301.      NC_array NC_attr  attrs        attributes for this variable
  302.      enum              type         data type for this variable (values as
  303.                                     for NC_array above)
  304.      u_long            len          total number of bytes on disk
  305.                                     The length of a netCDF record is the
  306.                                     sum of the len fields of all record
  307.                                     variables.
  308.      u_long            begin        disk address
  309.                                     (This is calculated on the basis of the
  310.                                      known data lengths in the Unidata code,
  311.                                      NOT obtained from xdr_getpos.)
  312.                                     All non-record variables precede all
  313.                                     record variables, to allow a the
  314.                                     block of record variables to be treated
  315.                                     as an array of an indeterminate
  316.                                     number of record structure instances.
  317.  
  318.  
  319.  2D. NC_dim format
  320.  -----
  321.  
  322.      NC_string         name         name associated with dimension
  323.      long              size         number of elements along dimension
  324.  
  325.    Note: A netCDF file may have zero or one UNLIMITED dimension, which is
  326. marked by size==0.  If a variable has the UNLIMITED dimension, that must
  327. be its slowest varying dimension.  Such variables are physically placed
  328. at the end of the data section, and numrecs copies of this "record section"
  329. exist at the end of the data section.  (The record variables may occur
  330. anywhere in the vars list of variables for the file.)
  331.  
  332.  
  333.  2E. NC_attr format
  334.  -----
  335.  
  336.      NC_string         name         name of attribute
  337.      NC_array <any>    data         value of attribute
  338.  
  339.    Notes: The data must be one of the "public" types (byte, char, short,
  340. long, float, or double).  An NC_attr written to the attrs array at the
  341. beginning of the netCDF file is a "global" attribute which applies to
  342. the whole file.  An NC_attr written to the attrs array in an NC_var
  343. applies only to that variable.
  344.  
  345.  
  346.  2F. NC_string format
  347.  -----
  348.  
  349.      u_long            count        number of characters in string
  350.      opaque            values       count characters
  351.  
  352.    Note: The count does NOT include any trailing '\0' character; a count
  353. of 0 is interpreted as (NC_string *)0, NOT as a zero-length string.
  354.  
  355.  
  356.  2G. NC_iarray format
  357.  -----
  358.  
  359.      u_long            count        number of 4-byte ints in array
  360.      int               values       count ints (written sequentially)
  361.  
  362.    Note: The count can be zero.
  363.  
  364. ------------------------------------------------------------------------------
  365.  
  366.  
  367.  
  368. 3. The PDB file format
  369. ----------------------
  370.  
  371.    The self-descriptive information in a netCDF file is stored using a
  372. variety of data types.  Thus, a name is a u_long count followed by the
  373. actual characters as an opaque, a disk address is a u_long, and a data
  374. type is an enum.  In contrast, the self-descriptive information in a
  375. PDB file is mostly character encoded, with certain characters set
  376. aside as delimiters of various sorts.  Hence, a disk address or a size
  377. is converted to the characters of the equivalent decimal number in the
  378. PDB file self-description.
  379.  
  380.    To describe such character encoded data, the following discussion
  381. adopts a notation based on the format argument to the standard C
  382. library routines printf and scanf.  The meaning of this notation is as
  383. follows: A quoted string represents a consecutive sequence of 8-bit
  384. bytes containing the ASCII representations for the characters in the
  385. string.  Thus,
  386.  
  387.      "Hello"
  388.  
  389. represents the five bytes 0x48, 0x65, 0x6c, 0x6c, 0x6f, in that order.
  390. The two characters "\" and "%" are exceptions to this rule.
  391.  
  392.    A "\" in a format string introduces an escape sequence which
  393. represents a single non-printable ASCII character.  The only escape
  394. sequences required for the following discussion are "\n", "\t",
  395. "\001", and "\002".  These have the following meanings:
  396.  
  397.      "\n" means a newline character, which can have any single one
  398.           of the three values 0x0a (ASCII line feed), 0x0d (ASCII
  399.           carriage return), or 0x1f (ASCII unit separator)
  400.           This non-unique choice for a delimiter character is an
  401.           inconvenient leftover from an early implementation of
  402.           the original PDBLib programming interface.
  403.      "\t" means a tab character, 0x09.
  404.      "\001" means and ASCII SOH character, 0x01.
  405.      "\002" means and ASCII STX character, 0x02.
  406.  
  407.    The "%" character in a format string introduces an escape sequence
  408. which represents the characters produced by converting data into a
  409. printable form.  The only such format conversions required in the
  410. following discussion are:
  411.  
  412.      "%d"  means the decimal equivalent of an int value, that is,
  413.            a sequence of digits possibly preceded by a minus "-".
  414.      "%ld" is the same thing for a long value
  415.      "%s"  means zero or more characters in a null-terminated string
  416.            of ASCII characters
  417.  
  418.    Because the bytes 0x01, 0x02, 0x0a, 0x0d, and 0x1f ("\001", "\002",
  419. and "\n") are used as delimiters, these five characters may not occur
  420. in any string output with a "%s" in the PDB file format.  A sixth
  421. character, 0x00, may not occur by the definition of "%s".  As with the
  422. netCDF file format, particular programming interfaces to read and
  423. write PDB files may impose stricter limitations on the set of
  424. characters which are legal in variable names and data type names.
  425. A discussion of restrictions of this sort will follow the PDB file
  426. format description.
  427.  
  428.  
  429.  3A. Whole-file format
  430.  -----
  431.  
  432.      "!<<PDB:II>>!\n"   HeadTok      13 bytes of identification
  433.      byte               count        byte count of prim_info + 1
  434.                                      normally count=
  435.                                        24+sizeof(float)+sizeof(double)
  436.      byte[count-1]      prim_info    parameterizations of short, int, long,
  437.                                      float, double, and * primitive types,
  438.                                      giving size, byte order, and floating
  439.                                      point layout
  440.      "%ld\001"          float_bias   exponent bias for float type
  441.      "%ld\001\n"        double_bias  exponent bias for double type
  442.      "%ld\001"          chart_addr   file byte address of structure chart
  443.      "%ld\001\n"        symtab_addr  file byte address of symbol table
  444.  
  445.      <any>              data         the binary data
  446.  
  447.      PDB_chart          chart        the structure chart defining compound
  448.                                      data types in terms of primitive data
  449.                                      types and simpler compound types,
  450.                                      begins at byte chart_addr of file
  451.      PDB_symtab         symtab       the symbol table associating a variable
  452.                                      name, type, and dimensions with a disk
  453.                                      address,
  454.                                      begins at byte symtab_addr of file
  455.      PDB_extras         extras       corrections to and extensions of the
  456.                                      PDB file format,
  457.                                      begins at byte immediately following
  458.                                      the symtab
  459.  
  460.    The prim_info array is broken down as follows:
  461.  
  462.      byte[6]            sizeof(void *), sizeof(short), sizeof(int),
  463.                         sizeof(long), sizeof(float), sizeof(double)
  464.                                      the number of bytes of six of the
  465.                                      seven predefined primitive data types
  466.                                      sizeof(char) is always 1 byte
  467.      byte[3]            orderof(short), orderof(int), orderof(long)
  468.                                      the byte order of the three multibyte
  469.                                      integer data types, 1 if the most
  470.                                      significant byte is first, 2 if the
  471.                                      least significant byte is first
  472.      byte[sizeof(float)]             permutation of bytes for float type
  473.      byte[sizeof(double)]            permutation of bytes for double type
  474.      byte[7]            bitsof(float)  bit sizes and addresses of sign,
  475.                                        exponent, and mantissa in a float
  476.      byte[7]            bitsof(double) bit sizes and addresses of sign,
  477.                                        exponent, and mantissa in a double
  478.  
  479.    The meaning of the permutation and bitsof(...) for the float and
  480. double types is fully described below in the section on PDB
  481. parameterization of floating point layout.
  482.  
  483.  
  484.  3B. PDB_chart format
  485.  -----
  486.  
  487.      The structure chart consists of a series of structure
  488. definitions, representing the various compound data types used to
  489. describe the data in the file.  Each structure definition begins with:
  490.  
  491.      "%s\001"           base_type_name
  492.                                      the name of the compound data type
  493.      "%ld\001"          size         byte size of one instance of the
  494.                                      data structure on disk
  495.  
  496. The definition continues with one member descriptor per member of
  497. the data structure:
  498.  
  499.      "%s\001"           descriptor   basically "type name(dimensions)",
  500.                                      described in detail below
  501.                                      An array of 12 arrays of 5 doubles
  502.                                      called "junk" would have the
  503.                                      descriptor "double junk(12,5)".
  504.  
  505. An individual structure definition ends with a newline character:
  506.  
  507.      "\n"               end_def      end of structure definition is
  508.                                      thus always "\001\n"
  509.  
  510. The end of the entire structure chart is marked by:
  511.  
  512.      "\002\n"           end_chart    This may occur before any structure
  513.                                      definitions, in which case all of
  514.                                      the variables in the file must have
  515.                                      one of the primitive data types.
  516.  
  517.    Data can be either an instance of one of the primitive data types,
  518. an instance of a compound data type, or a pointer to an object.  The
  519. data type of a pointer specified as a <full_type> string, which has
  520. the format
  521.  
  522.      <full_type> is
  523.         <ws><base_type_name><indirection_level>
  524.      where
  525.         <ws> is zero or more of the whitespace characters space or tab,
  526.              that is " " or "\t"
  527.         <indirection_level> is zero or more asterisk or whitespace
  528.              characters, that is "*" or " " or "\t".  The level of
  529.              indirection is the number of "*" characters; any
  530.              <full_type> with a level of indirection greater than
  531.              zero represents a pointer.
  532.  
  533.    The general format of a member descriptor is:
  534.  
  535.      <full_type><ws><member_name><dimlist>
  536.  
  537. where
  538.         <ws> is zero or more whitespace (" " or "\t") characters,
  539.              but at least one such character if the <indirection_level>
  540.              field of the <full_type> has zero characters
  541.         <full_type> is the data type of this member; its <base_type_name>
  542.              is either a primitive data type name, or the name of a
  543.              previously defined compound data type,
  544.         <member_name> is the member name associated with this descriptor,
  545. and     <dimlist> is either zero characters (if the member is a scalar), or
  546.              <open_dimlist><dimlist_interior><close_dimlist>
  547. where
  548.            <open_dimlist> is either "(" or "[",
  549.            <close_dimlist> is either ")" or "]", and
  550.            <dimlist_interior> is a comma "," delimited list of
  551.               or "%ld"       length          number of elements along
  552.                                              this dimension
  553.               or "%ld:%ld"   origin, max_i   origin is a suggested
  554.                                              minimum index value along
  555.                                              this dimension, and max_i
  556.                                              is origin+length-1, the
  557.                                              maximum index value along
  558.                                              this dimension
  559.  
  560.         The <dimlist_interior> may contain whitespace (" " or "\t")
  561.         characters anywhere except within the "%ld" fields.
  562.         For multidimensional lists, the dimensions are listed
  563.         slowest varying first (but see "Major-Order" in PDB_extras below).
  564.  
  565.    The names of the primtive data types are:
  566.  
  567.      "char"    same as C language char
  568.      "short"   same as C language short
  569.      "integer" same as C language int
  570.      "long"    same as C language long
  571.      "float"   same as C language float
  572.      "double"  same as C language double
  573.      "*"       similar to void* in C language, but requires a pointee
  574.                type when used as the type of a member or variable
  575.  
  576.  
  577.  3C. PDB_symtab format
  578.  -----
  579.  
  580.    The symbol table consists of a series of variable definitions which
  581. specify the variable name, data type, dimensions, and disk address.
  582. Each defintion has the following format:
  583.  
  584.      "%s\001"           name         the name of the variable
  585.      "%s\001"           full_type    the full data type name
  586.                                      This is of the form <full_type>
  587.                                      as described in PDB_chart above.
  588.      "%ld\001"          number       the total number of full_type
  589.                                      objects, which is the 1 or the
  590.                                      product of the dimension lengths
  591.      "%ld\001"          address      the byte address of the first
  592.                                      byte of this data in the file
  593.  
  594. The variable definition continues with one (origin, length) pair for
  595. each dimension associated with the variable.  As for the dimensions in
  596. a dimension descriptor, the slowest varying dimension is listed first
  597. (but see "Major-Order" in PDB_extras below).
  598.  
  599.      "%ld\001"          origin       suggested minimum index value for
  600.                                      this dimension
  601.      "%ld\001"          length       number of elements along this
  602.                                      dimension (NOT maximum index value
  603.                                      as in member descriptor)
  604.  
  605. The variable definition concludes with a newline:
  606.  
  607.      "\n"               end_def      end of variable definition is
  608.                                      thus always "\001\n"
  609.  
  610.    The end of the entire symbol table is marked by a second consecutive
  611. newline:
  612.  
  613.      "\n"               end_symtab   end of symbol table is thus always
  614.                                      "\n\n" (unless it is empty, which
  615.                                      is not a very interesting case)
  616.  
  617.  
  618.  3D. PDB_extras format
  619.  -----
  620.  
  621.    The extras section begins with the byte immediately following
  622. end_symtab, the end of the symbol table.  The extras section consists
  623. of a sequence of extra blocks.  Each extra block consists of a marker
  624. of the form:
  625.  
  626.      "%s:"              extra_id     name of the "extra"
  627.  
  628. followed by any amount of textual data (except for the "Alignment"
  629. extra_id, see below), and ending with:
  630.  
  631.      "\n"                            This is not necessarily the
  632.                                      first "\n" associated with the
  633.                                      extra_id, but if the extra_id was
  634.                                      not recognized, the characters
  635.                                      following a "\n" are scanned for
  636.                                      "%s:" before the next "\n" to try
  637.                                      to match a known extra_id
  638.  
  639. The end of the entire extras section is marked by a second consecutive
  640. newline:
  641.  
  642.      "\n"               end_extras   Unless the extras section is empty,
  643.                                      it therefore ends with "\n\n".
  644.  
  645.    The following extra_id names have meanings in version 7 PDB files:
  646. "Alignment", "Major-Order", "Primitive_Types", "Offset", "Version", and
  647. "Casts".  These are listed in rough order of importance for interpreting
  648. the data in a PDB file.  Here are the formats for these extras blocks:
  649.  
  650.      "Alignment:"                    begin block which gives the
  651.                                      alignments of the primitive
  652.                                      data types within structures
  653.      byte               char_align   alignment boundary for char
  654.      byte               ptr_align    alignment boundary for pointers (*)
  655.      byte               short_align  alignment boundary for short
  656.      byte               int_align    alignment boundary for int
  657.      byte               long_align   alignment boundary for long
  658.      byte               float_align  alignment boundary for float
  659.      byte               double_align alignment boundary for double
  660.      "\n"                            end block of alignments
  661.  
  662.      Note:  It would have been more consistent to print the alignments
  663.             in ASCII as "%d\001".  The 7-byte format shown above would
  664.             present a problem if any of the alignments happened to be
  665.             one of the ASCII newline characters.  In practice, this
  666.             doesn't ever happen, since the alignments are always
  667.             powers of two.
  668.  
  669.       The "Alignment" extra corrects a critical oversight in the PDB
  670.    prim_info (see whole file format above) data.  Namely, the byte offset
  671.    of a structure member cannot be calculated without knowing whether
  672.    the target machine/compiler places alignment restrictions on the
  673.    various primitive data types.  Therefore, until the "Alignment"
  674.    extra has been read, no compound data type defined in the structure
  675.    chart has a precise meaning.  And yes, it is annoying that the
  676.    extras section cannot be read before the symbol table, and the
  677.    data types used in the symbol table are defined in the structure
  678.    chart, and the structure chart cannot be interpreted without the
  679.    "Alignment" extra.
  680.  
  681.       Note that the disk address of a variable is NOT necessarily
  682.    aligned to be a multiple of the alignment of its data type.  Alignment
  683.    applies only to the offset of a structure member from the beginning of
  684.    a structure instance.  The alignment of a structure member which is
  685.    itself a compound data type is computed as the largest alignment of
  686.    any of its own members.  (There exist machines and compilers for which
  687.    this calculation is incorrect, but even on such machines, practical
  688.    examples of structures which fail the simple alignment calculation
  689.    required by the PDB file format are rare.)  In practice, alignments
  690.    are always powers of two, so the largest alignment of any member of
  691.    a structure is also the least common multiple of all the member
  692.    alignments.
  693.  
  694.  
  695.      "Major-Order:"                  begin Major-Order block
  696.      "%d\n"             dim_order    "101" if first dimension varies
  697.                                      slowest (default)
  698.                                      "102" if first dimension varies
  699.                                      fastest
  700.  
  701.       The "Major-Order" extra MUST be interpreted in order to make
  702.    sense of the structure chart and symbol table, since it changes the
  703.    meaning of the dimension lists in both structure member descriptors
  704.    and variable definitions.  If the "Major-Order" extra is not present,
  705.    the default is that the first dimension listed is the slowest varying
  706.    dimension, and the last dimension listed is the fastest varying.
  707.    Thus, a structure member with the descriptor "int x(2,3)" and the
  708.    default dim_order means that the six associated values are, in
  709.    order, x(0,0), x(0,1), x(0,2), x(1,0), x(1,1), x(1,2).  If the
  710.    dim_order is 102, the same descriptor describes the six values in
  711.    the order x(0,0), x(1,0), x(0,1), x(1,1), x(0,2), x(1,2).
  712.    Again, it is an annoyance that the "Major-Order" is known only
  713.    after the symbol table has been read.
  714.  
  715.       Unless the "Major-Order" extra is properly interpreted, the
  716.    topology of multidimensional arrays in a PDB file will be wrong.
  717.  
  718.  
  719.      "Primitive-Types:\n"            begin Primitive-Types block
  720.  
  721.    The Primitive-Types block is an adjunct to the structure chart,
  722.    which allows primitive data types other than char, short, int, long,
  723.    float, and double to be defined.  These primitive types can be used
  724.    as the <base_type_name> either in the symbol table, or in a member
  725.    descriptor in the structure chart, just like any other data type.
  726.    Each primitive type begins:
  727.  
  728.      "%s\001"           base_type_name
  729.                                      the name of the primitive data type
  730.      "%ld\001"          size         byte size of one instance of the
  731.                                      primitive type on disk
  732.      "%d\001"           alignment    the alignment; the byte offset of
  733.                                      a structure member of this data type
  734.                                      will always be a multiple of this
  735.      "%d\001"           order        1 if the most significant byte is
  736.                                      first, 2 if the least significant
  737.                                      byte is first, and -1 if f_flag is
  738.                                      "NO-CONV" or "FLOAT"
  739.  
  740.      "%s\001"           p_flag       "ORDER" if a byte order permutation
  741.                                      follows, else "DEFORDER"
  742.         "%d\001"[size]  permutation  if p_flag=="ORDER", the permutation
  743.                                      is listed as size ASCII numbers
  744.                                      These are in the same order as the
  745.                                      permutations in the prim_info above.
  746.  
  747.      "%s\001"           f_flag       "NO-CONV" if the data is opaque,
  748.                                      "FIX" if the data should be
  749.                                         transformed as an integer, and
  750.                                      "FLOAT" if the data should be
  751.                                         transformed as a floating point
  752.         "%ld\001"[8]    fp_format    if f_flag=="FLOAT", the 8 numbers
  753.                                      parameterizing the floating point
  754.                                      layout are listed as ASCII numbers
  755.                                      These are in the same order as the
  756.                                      floating point descriptions in
  757.                                      prim_info above, except that the
  758.                                      exponent bias is added as an eighth
  759.                                      element of fp_format.
  760.  
  761.      "\n"               end_primitive  ends the definition of this
  762.                                      primitive data type
  763.  
  764.       The end of the entire Primitive-Types block is marked by:
  765.  
  766.      "\002\n"                        If there are no additional primitive
  767.                                      types, the entire block is
  768.                                      "Primitive-Types:\n\002\n"
  769.  
  770.       Once again, the Primitive-Types information is required to
  771.    interpret the meaning of the structure chart and symbol table, but
  772.    cannot be read until after the symbol table.
  773.  
  774.  
  775.      "Offset:%d\n"      default_origin
  776.                                      Specifies the default dimension
  777.                                      origin for member descriptor
  778.                                      dimension lists in the structure
  779.                                      chart.  The default default_origin
  780.                                      is zero.
  781.  
  782.  
  783.      "Version:%d|%s\n"  version, date
  784.                                      PDB version number (7 for the format
  785.                                      described here) and file creation
  786.                                      date string
  787.  
  788.  
  789.      "Casts:\n"                      begin Casts block
  790.  
  791.    The Casts block provides additional information about members of
  792.    data structures which are pointers.  Specifically, another member
  793.    of the data structure may be of type char *, and point to a string
  794.    which is of the form <base_type_name><pointer_indicator>, which is
  795.    the "true data type" of the pointee (the <base_type_name> in the
  796.    first member descriptor is just a dummy, like void *).  Whether or
  797.    not this information is of any use depends on the programming
  798.    interface.  In any event, the only possible use is in writing new
  799.    instances of the structure, since on read, the "true type" of the
  800.    pointee is always known.  For each type-cast member of a data
  801.    structure, there is one entry of the form:
  802.  
  803.      "%s\001"           base_type    the <base_type_name> of the
  804.                                      data structure containing the
  805.                                      cast_member and type_member
  806.      "%s\001"           cast_member  the <member_name> of a member
  807.                                      with a pointer data type
  808.      "%s\001\n"         type_member  the <member_name> of a member
  809.                                      of type char *, whose value
  810.                                      (after dereference) contains
  811.                                      a string representing the
  812.                                      "true type" of the pointee
  813.                                      from the cast_member
  814.  
  815.    The end of the Casts block is marked by:
  816.  
  817.      "\002\n"                        If there are no casts, the entire
  818.                                      Casts block is "Casts:\n\002\n".
  819.  
  820.  
  821.    The end of the entire extras section is marked by a second consecutive
  822. newline:
  823.  
  824.      "\n"               end_extras   end of extras section is thus always
  825.                                      "\n\n" (unless it is empty)
  826.  
  827.  
  828.  3F. Pointee format
  829.  -----
  830.  
  831.    The PDB pointer/pointee format is not optimal, but it does model
  832. several of the most important practical uses of pointers in the C
  833. programming language.  Any variable with a full_type containing one or
  834. more trailing asterisk "*" characters is a pointer variable, and any
  835. member descriptor with asterisks preceding the <member_name> is a
  836. pointer member.  The pointer itself has no representation at all in
  837. the PDB file.  A pointer member has a size and alignment within its
  838. data structure, but the bytes stored there are garbage.  A pointer
  839. variable takes up no space at all on disk; its address is the address
  840. of the first pointee (the only pointee if the variable is a scalar).
  841.  
  842.    Every pointee consists of a descriptive header of indeterminate
  843. length, possibly followed by the pointee data itself.  A header-only
  844. pointee contains the disk address of the pointee which is followed by
  845. the data.  This allows multiple pointers to the same data without
  846. multiple disk copies of the data.  The format of a PDB pointee is:
  847.  
  848.      "%ld\001"          nitems       the number of objects in the pointee
  849.                                      (1 if it is a scalar; otherwise the
  850.                                       pointee is interpreted as a one
  851.                                       dimensional array)
  852.      "%s\001"           full_type    <base_type_name><pointer_indicators>
  853.                                      specifying the data type of the
  854.                                      pointee
  855.      "%ld\001"          address      disk address of beginning of this
  856.                                      pointee if data_here!=0, otherwise
  857.                                      the address of the pointee with
  858.                                      data_here!=0 containing the data
  859.      "%d\001\n"         data_here    1 if the first byte of the data
  860.                                      immediately follows the "\n",
  861.                                      0 if the address points to another
  862.                                      pointee, which is guaranteed to have
  863.                                      data_here==1
  864.      <any>              data         only present if data_here!=0
  865.  
  866.    A NULL pointer is marked by a pointee with nitems==0, address==-1,
  867. and data_here==0.
  868.  
  869.    The full_type of a pointee need not agree with the type expected
  870. from the nominal type of its pointer.  In effect, every PDB pointer is
  871. a C void *, since the pointee contains the data type and number of
  872. items.  (The original PDBLib programming interface, however, requires
  873. the Casts extra in order to be able to write data of a different type
  874. than expected on the basis of the pointer declaration; a pointer
  875. variable of a type different than one dereference of its full_type
  876. cannot be written at all with this interface.  The Casts extra is not
  877. required for PDBLib to correctly read "cast" data in either pointer
  878. variables or pointer members.  The features or limitations of a
  879. particular programming interface have no bearing on the format of a
  880. PDB file, in any event.)
  881.  
  882.    The major drawback of the PDB pointer/pointee format is that
  883. nothing at a known address actually points to the first pointee; the
  884. pointee addresses are stored only in the pointees themselves.  The
  885. address of a pointee is determined as follows:
  886.  
  887.    The address of the first pointee of a pointer variable is the
  888. address of the variable.  (Since the pointers themselves have no disk
  889. representation, they are not written).
  890.  
  891.    The address of the pointee corresponding to the first pointer
  892. member of the first element of an array of structure instances is the
  893. address of the byte immediately following the array of instances.
  894. (The structure instance array actually takes up space on disk, even if
  895. it consists entirely of pointers.  The size and alignment of pointer
  896. members are specified in the small header for the whole file and in
  897. the Alignment extra, respectively.  The value of the pointer member
  898. itself is meaningless.)  When the first pointee has been completely
  899. written, including any pointees corresponding to pointer members of
  900. its own data type, the pointee corresponding to the second pointer
  901. member of the first element of the structure instance array is written
  902. starting at the address following the first pointee and all its
  903. descendants.  This continues until the last pointer member of the
  904. first array element, after which comes the first pointer member of the
  905. second array element, and so on.
  906.  
  907.    The recursive algorithm used to write or read a PDB pointee can be
  908. schematically indicated by a recursive function pdb_object, which
  909. performs serial I/O on an array of nitems objects of type full_type,
  910. beginning at a specified disk address, and returning the address of
  911. the byte following what has just been read or written.  Any actual
  912. interface would be substantially more complicated than this, since
  913. additional input arguments would be required to specify data to be
  914. written, and additional output arguments would be required to return
  915. the data read.  Nevertheless, here is the schema:
  916.  
  917.      long pdb_object(full_type, nitems, address)
  918.      {
  919.        if ( is_a_pointer(full_type) ) {
  920.          while (nitems--) {
  921.            address= read_or_write_pointee_header(address);
  922.            if ( not_seen_before(address) )
  923.              address= pdb_object( dereference_type(full_type), 1, address );
  924.          }
  925.  
  926.        } else {
  927.          address= read_or_write_object_array( full_type, nitems, address );
  928.          while (nitems--) {
  929.            while (pointer= next_pointer_member(full_type)) {
  930.              address= pdb_object( pointer_type(pointer),
  931.                                   pointer_nitems(pointer), address );
  932.            }
  933.          }
  934.        }
  935.  
  936.        return address;
  937.      }
  938.  
  939.    Note that this algorithm does not permit partial read or write
  940. operations on array pointer variables or structure instances
  941. containing pointer members.  The addresses of the pointees are only
  942. revealed by performing the entire sequence of read or write operations
  943. on a complete variable.
  944.  
  945.  
  946.  3G. PDB parameterization of floating point layouts
  947.  -----
  948.  
  949.    Converting from one floating point format to another is far easier
  950. than converting from any floating point format into a textual
  951. representation of a number.  In general, a floating point conversion
  952. to any format is not much harder than a conversion to the particular
  953. big-endian IEEE format preferred by the netCDF format.
  954.  
  955.    The PDB parameterization of floating point layouts encompasses all
  956. machines where the floating point size is a multiple of an 8-bit byte,
  957. the exponent is binary, and the exponent and mantissa are contiguous
  958. sequences of bits for some permutation of the bytes.  Cray 128-bit
  959. floating point formats (which have two interrelated exponents) and the
  960. hex exponent formats used by a few old mainframes are the only
  961. significant floating point formats not covered by the PDB
  962. parameterization; such machines must convert their internal formats to
  963. a form that is covered in order to read or write a PDB file, just as
  964. they must do a conversion to read or write a netCDF file.
  965.  
  966.    The PDB parameterization has three parts: The permutation, the
  967. specification of the bit addresses and bit sizes of the sign,
  968. exponent, and mantissa, and the bias of the exponent.  Using the same
  969. notation as in the XDR RPC1014 protocol specification (sections 3.6
  970. and 3.7), the value of a floating point number with sign S, exponent
  971. E, and mantissa (or fractional part) F is
  972.  
  973.      (-1)^S * 2^(E-bias) * 1.F
  974.  
  975. where ^ represents exponentiation, * represents multiplication, and
  976. 1.F means 1 + (F / 2^(number of bits in mantissa)).  Zero is always
  977. represented by all bits of S, E, and F zero.  There must be some
  978. permutation of the bytes of the floating point number such that the
  979. bytes containing E (and F) are contiguous and ordered from most
  980. significant bits of E (and F) to least significant.  In this big-endian
  981. style order, the bits can be numbered from zero to one less than eight
  982. times the number of bytes.  The S, E, and F fields can then be
  983. described by specifying the bit on which they start (bit addresses),
  984. and the number of bits over which they extend (bit size).  The sign
  985. always has a bit size of 1.
  986.  
  987.    In a PDB file, the permutation is a list of all the numbers from 1
  988. to sizeof(float) or sizeof(double) where the value 1, 2, 3, and so on
  989. represents the location of a byte in the standard big-endian byte
  990. order defined above, and the position of the value in the list
  991. represents the position of that byte in the actual floating point
  992. number.  Hence, the permutation of a for a float on a Sun SPARCstation
  993. or in an XDR file is {1, 2, 3, 4} (standard big-endian order), while
  994. the permutation of a float on a DECstation 3100 is {4, 3, 2, 1}
  995. (standard little-endian order).  The VAX is the only machine with
  996. floating point formats having a non-monotonic permutation.  A VAX is a
  997. little-endian machine, but its floating point format is big-endian
  998. with respect to 2-byte words, with each 2-byte word little-endian.
  999. The resulting PDB permutation for a VAX float is {3, 4, 1, 2}.
  1000.  
  1001.    The PDB convention for specifying floating point bit sizes and
  1002. addresses is:
  1003.  
  1004.      byte        bits_per_word       8 * number of bytes (redundant)
  1005.      byte        exponent_size       number of bits in exponent
  1006.      byte        mantissa_size       number of bits in mantissa
  1007.      byte        sign_address        bit address of sign (in standard
  1008.                                      byte order as described above)
  1009.      byte        exponent_address    bit address of exponent
  1010.      byte        mantissa_address    bit address of exponent
  1011.      byte        mantissa_flag       0 if high order bit of mantissa
  1012.                                      is preceded by implicit 1 (1.F)
  1013.                                      1 if high order bit of mantissa
  1014.                                      is explicitly the 1 (always set
  1015.                                      except in representation of 0.0)
  1016.      long        exponent_bias       bias of the exponent (must be less
  1017.                                      than 2^31 to fit into a long on all
  1018.                                      machines, but this is not a
  1019.                                      practical limitation)
  1020.  
  1021.    The mantissa_flag allows for one more difference between floating
  1022. point formats: the 1 in 1.F is sometimes explicitly included as the
  1023. first bit of F.  This is the case for the Cray floating point format,
  1024. and for 10 and 12 byte floating point formats on several platforms.
  1025.  
  1026.    A few explicit examples of this parameterization should remove all
  1027. doubts about its meaning:
  1028.  
  1029.                      #E  #F  &S &E &M  1? bias
  1030.         float   {32,  8, 23, 0, 1,  9, 0,   127}  (netCDF/XDR standard)
  1031.         double  {64, 11, 52, 0, 1, 12, 0,  1023}  (netCDF/XDR standard)
  1032.              (Above two cover the vast majority of modern machines,
  1033.               which are distinguished only by the permutation.)
  1034.         float   {64, 15, 48, 0, 1, 16, 1, 16384}  (Cray 1, XMP, YMP)
  1035.         float   {32,  8, 23, 0, 1,  9, 0,   129}  (VAX)
  1036.         double  {64,  8, 55, 0, 1,  9, 0,   129}  (VAX H-format)
  1037.         double  {64, 11, 52, 0, 1, 12, 0,  1025}  (VAX G-format)
  1038.              (permutations of VAX doubles are {2, 1, 4, 3, 6, 5, 8, 7})
  1039.         double  {96, 15, 64, 0, 1, 32, 1, 16382}  (MacIntosh long double)
  1040.              (Bits 16-31 unused in this format.)
  1041.  
  1042.    Note that the permutation is not uniquely specified for the Cray
  1043. and MacIntosh long double formats.  In such a case, the closest
  1044. permutation to one of the monotone permutations should be selected.
  1045. Another way to say this is to require that in the standard big-endian
  1046. order, the E (exponent) field should always have a smaller bit address
  1047. than the F (mantissa) field, and the location of any unused bytes
  1048. relative to E and F should be preserved.
  1049.  
  1050.  
  1051.  3H. Restrictions on characters used in names
  1052.  -----
  1053.  
  1054.    The use of "\001", "\002", and "\n" as separators in PDB string
  1055. formats precludes their use in variable names, type names, or member
  1056. names.  Using the null byte 0x00 in any name would make it far more
  1057. difficult to write a C program to access PDB files, so this character
  1058. is illegal in any names as well.  Hence 0x00, 0x01, 0x02, 0x0a, 0x0d,
  1059. and 0x1f may never appear in any PDB string converted with %s in the
  1060. above description.
  1061.  
  1062.    Another absolute proscription follows from the method used to
  1063. indicate the type of a pointer variable, namely, a data type name
  1064. (either from the structure chart or from the Primitive-Types extra
  1065. block) may not contain any space, tab, or asterisk characters, " ",
  1066. "\t", or "*", that is 0x20, 0x09, or 0x2a.
  1067.  
  1068.    For the same reason, structure member names may not include spaces,
  1069. tabs, or asterisks.  Additionally, no structure member name may
  1070. contain either character marking the beginning of the dimension list,
  1071. "(" or "[", that is, 0x28, or 0x5b.
  1072.  
  1073.    A more generic limitation is that no "\n"-terminated sequence of
  1074. characters in the extras section begin with "%s:" where the string
  1075. could be mistaken for any present or future extra_id name.  For
  1076. example, a data type name which appears in the Casts extra block had
  1077. better not have a name like "Alignment:".  This ugly possibility can
  1078. be eliminated by more careful design of future extra block formats; if
  1079. such a measure is not taken, then everyone sharing PDB files will need
  1080. to be sure they are using the same version of the programming
  1081. interface.  For now, this is not really a practical problem.
  1082.  
  1083.    One final warning is that the original PDBLib programming interface
  1084. imposes naming restrictions beyond those intrinsic to the PDB file
  1085. format, as just described.  These are as follows: the proscription of
  1086. the characters "(" and "[" used to introduce dimension lists is
  1087. extended to variable names in addition to structure member names.
  1088. Furthermore, the period, "." or 0x2e is illegal both in variable names
  1089. and in structure member names.  (These restrictions arise because the
  1090. PDBLib interface cracks ASCII text strings to perform partial read and
  1091. write operations.  If non-text-based partial read and write functions
  1092. were available, the additional restrictions on characters in file
  1093. names would disaapear.)
  1094.  
  1095.    The restrictions on characters used in PDB names are summarized in
  1096. the following table:
  1097.  
  1098.                                   variable   data type   structure member
  1099.                                    names      names        names
  1100.  
  1101.      0x00, 0x01, 0x02,             NEVER      NEVER        NEVER
  1102.      0x0a CR, 0x0d LF, 0x1f US
  1103.  
  1104.      0x09 TAB, 0x20 SPACE            -        NEVER        NEVER
  1105.      0x2a "*"
  1106.  
  1107.      0x28 "(", 0x5b "["             BAD         -          NEVER
  1108.  
  1109.      0x2e "."                       BAD         -           BAD
  1110.  
  1111. Here "BAD" means that the highest level functions in the original PDBLib
  1112. programming interface will fail.  Needless to say, the best idea is to
  1113. avoid any character in the above list under all circumstances.
  1114.  
  1115. ------------------------------------------------------------------------------
  1116.  
  1117.  
  1118.  
  1119. 4. A Generic Binary Data Description Language
  1120. ---------------------------------------------
  1121.  
  1122.    Unidata has provided a plain-text representation for netCDF files
  1123. (CDL format) which is extremely useful.  The following plain-text
  1124. format can of describe either a netCDF or a PDB file (as well as HDF
  1125. and the majority of one-of-a-kind binary file formats which have been
  1126. designed to store scientific data).
  1127.  
  1128.    The PDB format provides two capabilities lacking in the netCDF
  1129. format: non-XDR primitive data formats, and definable data types
  1130. including compound data structures and additional primitive types.
  1131. The netCDF format offers two capabilities lacking in the PDB format:
  1132. history records, and variable attributes.
  1133.  
  1134.    In a PDB file, the want of a formal provision for history records
  1135. or for variable attributes can be fulfilled by variable naming
  1136. conventions.  The trick is to use special naming conventions to
  1137. associate related variables ("x-units" might be the variable used to
  1138. store the "units" attribute of the variable "x"), or to imbue a
  1139. special significance to some of the variables in the file (instances
  1140. of a history record sequence might be named "rec0000", "rec0001", and
  1141. so on).  Such tricks result in data which is accessed nearly as
  1142. efficiently as with the netCDF interface to a netCDF file.
  1143.  
  1144.    Similar tricks allow single instances of data structures, such as
  1145. FORTRAN common blocks, to be accessed in a netCDF file. However, an
  1146. array of structure instances is very difficult to handle in netCDF
  1147. format, and any file with primitive data formats other than XDR is
  1148. impossible.
  1149.  
  1150.    Of course, the underlying simplicity of the netCDF format is really
  1151. a virtue, not a weakness.  A physics code written in FORTRAN can't
  1152. really generate any data the netCDF format can't handle.  In fact, in
  1153. designing a PDB format suitable for holding restart or post-processing
  1154. information for a physics code, one of the primary objectives is to
  1155. remain within the bounds of the data describable by the netCDF format.
  1156.  
  1157.    Two considerations beyond the scope of the format of an individual
  1158. file are important.  The first is robustness against unexpected
  1159. program or machine crashes when part, but not all, of the data in a
  1160. file has been written.  The second is the difficulty of handling
  1161. extremely large files as a single unit -- it is much easier to deal
  1162. with a few dozen smaller files than one monster.  Both problems
  1163. commonly arise with files used for storing history data, which is
  1164. generally stored one record at a time with a long pause between the
  1165. writing of one record and the next, and which may grow to a very large
  1166. size.
  1167.  
  1168.    The usual PDB format, which places the self-descriptive information
  1169. at the end of the file, is not very robust.  Unless the structure
  1170. chart, symbol table, and extras are written after each history record,
  1171. only to be overwritten by the next, a crash causes the data in the
  1172. file to be uninterpretable.  The file format itself allows the
  1173. structure chart, symbol table, and extras section to precede the data,
  1174. as in a netCDF file; perhaps such a strategy should be adopted to deal
  1175. with this problem.
  1176.  
  1177.    A very general way to deal with the problem of robustness is to
  1178. keep two files open.  Whenever new data is written at the end of the
  1179. data file, its description is written at the end of the description
  1180. file.  The files can be merged when they are really finished, or left
  1181. as eparate components of a single whole.  This two-file scheme has the
  1182. advantage that new data can be declared after some data has been
  1183. written, without sacrificing robustness in the face of program or
  1184. machine crashes.  The plain-text binary data description language
  1185. defined below is a designed to be usable as the format for the
  1186. description member of such a pair.
  1187.  
  1188.    The problem of dealing with very large files is easy to handle in
  1189. the case of history data -- just produce a family of files each having
  1190. a restricted length, instead of a single giant file.  Splitting
  1191. history data across several files has a major impact on the
  1192. programming interface used to access the data, but no effect on the
  1193. format of an individual file.  The most important thing to notice here
  1194. is that an attractive feature of the netCDF programming interface --
  1195. that you can retrieve values of a particular variable over a range of
  1196. times with a single subroutine call -- becomes much more difficult to
  1197. implement if the data extends over several files.  Another remark is
  1198. that it is wise to copy the non-record data into each file in a
  1199. history family, so that each file "makes sense" by itself.
  1200.  
  1201.    Bearing all of these remarks in mind, here is a generic binary data
  1202. description language, hereby christened "Clog", which can describe the
  1203. contents of any PDB or netCDF file, as well as many other binary file
  1204. formats:
  1205.  
  1206.  
  1207.  4A. Notation
  1208.  -----
  1209.  
  1210.    The extended Backus-Nauer Form notation used in the RFC1014 XDR
  1211. standard is adopted here:
  1212.  
  1213.    1. The characters |, (, ), [, ], and * are special.
  1214.    2. Terminal symbols are strings surrounded by double quotes.
  1215.    3. Non-terminal symbols are strings of non-special characters.
  1216.    4. Alternative items are separated by the vertical bar character |.
  1217.    5. Optional items are enclosed in square brackets [ ... ].
  1218.    6. Items are grouped by enclosing them in parentheses ( ... ... ).
  1219.    7. An item followed by * means zero or more occurrences of that item.
  1220.    8. A non-terminal followed by : and a set of alternatives constitutes
  1221.       the definition of that non-terminal.
  1222.  
  1223.    Comments in a binary data description file begin with /* and end
  1224. with */, and are treated as whitespace.  An identifier is a letter or
  1225. underscore "A-Za-z_", followed by zero or more letters, digits,
  1226. underscores, pluses, minuses, periods, or commas "A-Za-z_0-9,.+-".  An
  1227. identifier may also consist of a quoted string, which is interpreted
  1228. as the characters within the quotes, recognizing the following escape
  1229. sequences:
  1230.  
  1231.      \"    double quote
  1232.      \\    one backslash
  1233.      \ooo  an arbitrary 8-bit byte, except that \000 and any following
  1234.            characters are ignored
  1235.  
  1236. An identifier may not be more than 1023 characters long, in its printed
  1237. form including the open and close quotes, if any.
  1238.  
  1239.    A number is a sequence of one or more decimal digits optionally
  1240. preceded by a minus "-".  A float_number is anything readable by the
  1241. standard C library "%e" format directive.  All control characters and
  1242. spaces are treated as whitespace, principally to allow for any
  1243. differences in the newline character among various operating systems.
  1244.  
  1245.  
  1246.  4B. Overview of language
  1247.  -----
  1248.  
  1249.    The binary data description language Clog is modeled on C variable
  1250. declaration and structure definition syntax.  C declarations relate a
  1251. variable name, data type and dimension information.  Clog variable
  1252. declarations must additionally specify the disk address for the
  1253. variable.  Clog structure defintions are similarly extended to allow
  1254. the offset of each member to be specified.  This extension makes it
  1255. easier to automatically generate a Clog description of some binary
  1256. file formats.
  1257.  
  1258.    Following the PDB file format, Clog allows the bit-by-bit format of
  1259. the primitive data types to be specified.  This greatly increases the
  1260. set of binary files describable using Clog.  This same mechanism
  1261. enables new primitive data types to be declared.
  1262.  
  1263.    Following the netCDF file format, Clog has a formal mechanism for
  1264. describing a sequence of history records.  This allows natural
  1265. descriptions of an important class of binary files (including netCDF
  1266. files).
  1267.  
  1268.    Finally, the Clog contains a formal means for including new types
  1269. of descriptive information not envisioned at its inception.  (This has
  1270. been a valuable feature of the PDB file format.)  Separate "trial" and
  1271. "standard" extension syntax is provided.  The rules for Clog
  1272. extensions are simple: No Clog extension can alter the meaning of any
  1273. previously defined part of Clog (thus, nothing like the "Major-Order"
  1274. extra block in the PDB file format is acceptable).  And any Clog
  1275. extensions should be conceived as supplying supplemental information,
  1276. rather than as wholesale replacements of existing features to get
  1277. additonal functionality.
  1278.  
  1279.  
  1280.  4C. Basic primitive data types
  1281.  -----
  1282.  
  1283.    There are six basic primitive data types:
  1284.  
  1285.      char    an 8-bit byte
  1286.      short   a signed integer of at least 2 bytes
  1287.                - used when small size is important
  1288.      int     a signed integer of at least 2 bytes
  1289.                - most efficient integer type, used for boolean values
  1290.      long    a signed integer of at least 4 bytes
  1291.                - most commonly used integer type, e.g.- an array index
  1292.      float   a floating point number of at least 4 bytes, range is
  1293.                at least 10^(+-38), precision at least 6 decimal digits
  1294.      double  a floating point number usually 8 bytes, range is
  1295.                at least 10^(+-38), precision at least 9 decimal digits,
  1296.                but usually at least 10^(+-307) and 14 decimal digits
  1297.  
  1298.    No data structure (compound data type) or additional primitive may
  1299. have one of these six names.  Any one of these six names may be used
  1300. as a data type without any definition.  All other identifiers used as
  1301. data type names must be previous defined, with the following two
  1302. exceptions:
  1303.  
  1304.      string   a string of 8-bit ASCII characters not containing '\0'
  1305.                 - a string is represented as a long containing the
  1306.                   disk address of the string; the string itself is
  1307.                   a long (aligned as a long) with a non-negative
  1308.                   count of the non-0 characters in the string (i.e.-
  1309.                   the result of the ANSI C strlen function), followed
  1310.                   by that many characters.
  1311.      pointer  a pointer to an array of any type
  1312.                 - the pointee contains the data type and dimension
  1313.                   information; the pointer is represented as a long
  1314.                   containing the disk address of the pointee
  1315.  
  1316. If string or pointer is used as a data type without defining it, the
  1317. default meaning is as shown.  Once used, the string or pointer data
  1318. type may not be redefined.  A NULL pointer or string is represented by
  1319. a disk address of -1; there is no associated pointee.
  1320.  
  1321.  
  1322.  4D. Clog file layout
  1323.  -----
  1324.  
  1325.      clog_description:
  1326.         "Contents Log"
  1327.         basic_statement*
  1328.             [ record_initializer basic_statement* record_declaration* ]
  1329.             [ end_of_data ]
  1330.  
  1331.      basic_statement:
  1332.         primitive_definition
  1333.       | structure_definition
  1334.       | variable_declaration
  1335.       | alignment_spec
  1336.       | other_information
  1337.  
  1338.      record_initializer:
  1339.         record_declaration
  1340.       | record_begin
  1341.  
  1342.      end_of_data:
  1343.         "+" "eod" "@" disk_address
  1344.  
  1345.    The clog_description must begin with the QUOTED string "Contents
  1346. Log" -- if it is not quoted, the Clog lexical rules will divide it
  1347. into two tokens.  Note that comments and white space may precede the
  1348. "Contents Log" token.
  1349.  
  1350.    The clog_statement's order is restricted by a general "definition
  1351. before use" rule, as described in detail below.  Basically, this means
  1352. a data type must be defined before it can be used.  If the
  1353. record_initializer is present, clog_statements before it describe
  1354. non-record variables, while clog_statements afterwards describe record
  1355. variables.  Notice that all record_declaration statements, except
  1356. possibly the first, follow the clog_statements which declare the
  1357. record variables.  Thus, the structure of the records in a Clog
  1358. description cannot change.
  1359.  
  1360.    If present, the +eod statement must be the very last thing in a
  1361. Clog description of a file, even beyond any comments.  The
  1362. disk_address specified is the address of the first byte beyond all of
  1363. the data in the file.  This may be beyond the end of any variable
  1364. declared in the Clog for any number of reasons; the most obvious is
  1365. that there may be pointees beyond the last declared data.  The entire
  1366. +eod statement from the initial "+" to the final digit of the
  1367. disk_address must not occupy more than 80 characters.
  1368.  
  1369.    In addition to specifying a safe address at which to begin adding
  1370. data to the binary file, the +eod statement allows the entire Clog to
  1371. be appended to the end of the binary file itself to make a single,
  1372. self-descriptive package.  (Note that this procedure does not damage
  1373. either a netCDF or a PDB file.)  This is done as follows:
  1374.  
  1375.    At the address specified in the +eod statement, write the entire
  1376. plain-text Clog description of the file, including the final +eod
  1377. statement.  Then close and truncate the file.  A program which opens
  1378. the file can scan the last 80 bytes; if it finds a +eod statement, it
  1379. can check that "Contents Log" is the first token after specified
  1380. address, and, if so, interpret the Clog to determine the layout of the
  1381. binary data in the file.
  1382.  
  1383.  
  1384.  4E. Variable declaration
  1385.  -----
  1386.  
  1387.      variable_declaration:
  1388.         type_name variable_name dimension_spec* ["@" disk_address]
  1389.              ("," variable_name dimension_spec* ["@" disk_address])*
  1390.  
  1391.      type_name: identifier
  1392.  
  1393.      variable_name: identifier
  1394.  
  1395.      dimension_spec:
  1396.         "[" dimension_length [dimension_name] "]"
  1397.       | "[" minimum_index ":" maximum_index [dimension_name] "]"
  1398.  
  1399.      dimension_name: identifier
  1400.  
  1401.      dimension_length: number
  1402.  
  1403.      minimum_index: number
  1404.  
  1405.      maximum_index: number
  1406.  
  1407.      disk_address: number
  1408.  
  1409.  
  1410.    The disk_address is a byte address.  If omitted, the default is the
  1411. next available address after all the variables previously declared.
  1412. The "next available" address may be rounded up for alignment purposes,
  1413. as discussed in more detail below.
  1414.  
  1415.    If more than one dimension_spec is present, the slowest varying
  1416. dimension is first in the list, and the fastest varying dimension
  1417. last.  This is the C convention.  As in C, a multidimensional array is
  1418. best regarded as an array of arrays -- the first index specifies which
  1419. array, so the second index must vary faster.
  1420.  
  1421.    The optional dimension_name is provided for easier compatibility
  1422. with the netCDF format.  Behavior is undefined if the same
  1423. dimension_name is used for dimension_spec's with different lengths.
  1424. The idea is to distinguish between dimension lengths which are
  1425. accidentally equal, and those which are equal by virtue of their
  1426. variable's meanings.  If not suppied, the default dimension name is
  1427. "_%ld", where "%ld" is the decimal representation of the dimension
  1428. length.
  1429.  
  1430.    The alternative minimum_index:maximum_index syntax is intended to
  1431. suggest the preferred range of the index values.  The equivalent
  1432. dimension_length is maximum_index-minimum_index+1.  This information
  1433. is far less important than the dimension_length, since the
  1434. dimension_length values (in their proper order!) specify the topology
  1435. of the array -- that is, how to find nearest neighbors along the
  1436. various dimensions.
  1437.  
  1438.    The type_name and variable_name identifiers must be unique among
  1439. all other type_name and variable_name identifiers, respectively.
  1440. However, there are two separate name spaces, so a type_name identifier
  1441. may match a variable_name identifier without conflict.  The
  1442. dimension_name identifiers, if any, form a third independent name
  1443. space.
  1444.  
  1445.  
  1446.  4F. Primitive data type definition
  1447.  -----
  1448.  
  1449.      primitive_definition:
  1450.         "+" "define" type_name
  1451.             "[" size_value "]" "[" alignment_value "]"
  1452.             [ "[" order_value "]"
  1453.               ["{" sign_address exponent_address exponent_size
  1454.                    mantissa_address mantissa_size mantissa_flag
  1455.                    exponent_bias "}"] ]
  1456.       | "+" "define" "string" "standard"
  1457.       | "+" "define" "pointer" "standard"
  1458.  
  1459.      size_value: number
  1460.      alignment_value: number
  1461.  
  1462.      order_value:
  1463.         number
  1464.       | "sequential"
  1465.       | "pdbpointer"
  1466.  
  1467.      sign_address: number
  1468.      exponent_address: number
  1469.      exponent_size: number
  1470.      mantissa_address: number
  1471.      mantissa_size: number
  1472.      mantissa_flag: number
  1473.      exponent_bias: number
  1474.  
  1475.  
  1476.    The type_name must neither have been defined nor referenced
  1477. previously.
  1478.  
  1479.    All primitive type definitions must have a size_value (the number
  1480. of bytes one instance occupies) and an alignment_value (the largest
  1481. number by which the byte offset a structure member of this type is
  1482. guaranteed to be divisible).
  1483.  
  1484.    The order_value, if a number, determines the byte ordering within
  1485. the size_value.  If order_value is not present, the primitive type is
  1486. to be regarded as opaque.  If order_value is present, but { ... } is
  1487. not present, the primitive type is an integer value.  If { ... } is
  1488. present, the primitive type is a floating point value (order_value
  1489. must be present also in this case).
  1490.  
  1491.    If the order_value is the identifier "sequential", then any
  1492. instance of this primitive requires sequential I/O; that is, portions
  1493. of arrays of this type may not be read or written.  If the size_value
  1494. is 0, then the size of an instance is indeterminate.  Otherwise, an
  1495. instance of the object occupies the specified size and has the
  1496. specified alignment as a structure member.  The code reading or
  1497. writing the file is responsible for recognizing the name of a
  1498. sequential primitive and taking appropriate action to read or write
  1499. it.
  1500.  
  1501.    The parameterization of a floating point format is the same as
  1502. described above for the PDB file format.  The order_value is a
  1503. simplification of the general byte permutation provided by the PDB
  1504. file format.  The meaning of the order value is as follows:
  1505.  
  1506.      order_value  ==> 1. The magnitude of the order_value represents
  1507.                          the number of bytes per "word".  Within a
  1508.                          word, the byte order is monotone (either from
  1509.                          most significant to least significant or vice
  1510.                          versa).  The magnitude of the order_value is
  1511.                          thus a multiple of the size_value.  If the
  1512.                          entire object has monotone byte order, then
  1513.                          the magnitude of the order_value is one.  In
  1514.                          practice, this is always the case, except for
  1515.                          the VAX floating point formats.
  1516.                       2. The sign of the order value determines the
  1517.                          word order.  The byte order within a word is
  1518.                          always opposite to the word order.  (Otherwise
  1519.                          the entire word is monotone, the word size is
  1520.                          one, and the word order is the byte order.)
  1521.                          The sign is positive if the most significant
  1522.                          word is first, negative if the least
  1523.                          significant word is first.
  1524.                       3. An order_value of zero is equivalent to omitting
  1525.                          the order_value altogether; it indicates opaque
  1526.                          data with the specified size and alignment.
  1527.  
  1528. In brief, the vast majority of numeric formats fall into one of the
  1529. following four categories:
  1530.      order_value=  1  for big-endian (MSB first) machines
  1531.      order_value= -1  for little-endian (LSB first) machines
  1532.      order_value=  0  for opaque data
  1533.      order_value=  2  for VAX floating point formats
  1534.  
  1535.    The precise order of definition of primitive types is significant if
  1536. the file contains pointers to objects of non-predefined types.  In
  1537. this case, the data type of the pointee will be encoded as an ordinal
  1538. based on the order of +struct and +define definitions.  The exception
  1539. is a +define with a type_name of one of the predefined types: char,
  1540. short, int, long, float, double, string, or pointer.  The order of
  1541. such a +define is unimportant, provided only that it precede the first
  1542. use of that predefined type.  As explained in the next section, a
  1543. +define of type long must also precede any uses of the predefined
  1544. string or pointer types.
  1545.  
  1546.    To use the default definitions of string and pointer, you must NOT
  1547. specify them using a +define (doing so would redefine them to the
  1548. meaning specified in the +define), OR you must use the special
  1549. "standard" forms of +define.  With their default meanings, the a
  1550. string and a pointer are represented as a long.
  1551.  
  1552.    In general, +defines of the six basic primitive data types should
  1553. precede any other statements in the Clog description of a file.  If
  1554. these are not defined, the default is the standard for the machine on
  1555. which the binary data file was written.  Without the basic primitive
  1556. type definitions, therefore, a Clog description of a file is not
  1557. portable across different machine architectures.  As an example, a
  1558. Clog describing a netCDF file (in XDR format) would begin:
  1559.  
  1560.      +define char   [1][4][1]
  1561.      +define short  [2][4][1]
  1562.      +define int    [4][4][1]
  1563.      +define long   [4][4][1]
  1564.      +define float  [4][4][1] {0 1  8  9 23 0  127}
  1565.      +define double [8][4][1] {0 1 11 12 52 0 1023}
  1566.  
  1567. Note that, since a netCDF does not support data structures, the
  1568. alignment_value is not really significant.  For the same reason, the
  1569. definition of int is unnecessary.  Furthermore, a definition of a
  1570. synonym for char would be appropriate:
  1571.  
  1572.      +define byte   [1][4][1]
  1573.  
  1574.  
  1575.  4G. Compound data structure definition
  1576.  -----
  1577.  
  1578.      structure_definition:
  1579.         "+" "struct" type_name "{"
  1580.                         full_member_definition member_definition* "}"
  1581.  
  1582.      full_member_definition:
  1583.         type_name member_name dimension_spec* ["@" byte_offset]
  1584.  
  1585.      member_definition:
  1586.         full_member_definition
  1587.       | "," member_name dimension_spec* ["@" byte_offset]
  1588.  
  1589.      member_name: identifier
  1590.  
  1591.  
  1592.    The leading "+" permits immediate recognition a structure_definition
  1593. as opposed to a variable_declaration, without the necessity for making
  1594. "struct" a reserved word in the context of a type_name.
  1595.  
  1596.    If present, the byte_offset specifies the byte offset of the member
  1597. from the beginning of an instance of the data structure.  If absent,
  1598. the byte offset is the next available byte beyond all previously
  1599. declared members; the first member has a byte_offset of 0 by default.
  1600. The "next available" byte offset is always rounded up to the nearest
  1601. multiple of the alignment value for the type_name of the member.
  1602. The alignment value of a compound structure is the largest alignment
  1603. value for any of its members (see the discussion of alignment within
  1604. data structures below).  Despite the fact that the byte_offset syntax
  1605. allows it, no two members of a data structure may overlap.
  1606.  
  1607.    The body of each structure definition has its own name space, so
  1608. the member_name need only be unique among all the member_names for
  1609. the structure currently being defined.
  1610.  
  1611.    As usual, type_name in a member_definition must be either a
  1612. predefined primitive, or must have been previously defined.
  1613. Obviously, a structure may not contain members which are instances of
  1614. itself.
  1615.  
  1616.    Beyond this "define before using" requirement, the precise order of
  1617. defintion of structures is significant if the file contains pointers
  1618. to objects which are structures.  In this case, the data type of the
  1619. pointee will be encoded as an ordinal based on the order of +struct
  1620. and +define definitions.
  1621.  
  1622.  
  1623.  4H. Record definition
  1624.  -----
  1625.  
  1626.      record_begin:
  1627.         "+" "record" "begin"
  1628.  
  1629.      record_declaration:
  1630.         "+" "record" "{" [time_value] "," [cycle_value] "}"
  1631.                       ["@" disk_address]
  1632.  
  1633.      time_value: float_number
  1634.  
  1635.      cycle_value: number
  1636.  
  1637.    The first occurrence of +record changes the meaning of subsequent
  1638. variable_declaration statements from declaring non-record variables to
  1639. declaring record variables.  This first occurance may actually declare
  1640. the first history record itself, or it may merely be the record_begin
  1641. marker.
  1642.  
  1643.    In any record_declaration, the disk_address defaults to the next
  1644. available address, as for a variable declaration.
  1645.  
  1646.    The time_value and the cycle_value need not actually represent the
  1647. time and cycle number associated with the record; they can be any
  1648. double and long value which characterizes a record.  Note that the
  1649. time_value is specified only to the accuracy it is printed; if a
  1650. precise time is required, the time should be made a record variable.
  1651. The intent of time_value and cycle_value is to provide more
  1652. informative "names" for the record than merely its position in the
  1653. sequence of records.  If either time_value or cycle_value is omitted
  1654. in the first history record instance declaration, it must be omitted
  1655. in all following declarations; similarly, if present in the first
  1656. declaration, it must be present in all subsequent declarations.
  1657.  
  1658.    Because of the possibility of families of time history files, it is
  1659. very difficult to realize a programming interface which allows
  1660. non-record data to be written after the writing of records has begun.
  1661. This practical difficulty is the reason for the division of the Clog
  1662. description into a non-record section, followed by a record section.
  1663.  
  1664.    The restriction of history data to a sequence of a single type of
  1665. record, rather than allowing several interleaved sequences of records
  1666. of various types, is also deliberate.  If several types of record need
  1667. to be written, several output files or file families should be used.
  1668.  
  1669.    Note that a member of the history record structure may be a pointer
  1670. to a block of data whose size changes from record to record.  For this
  1671. reason, Clog records are not necessarily layed out end-to-end in the
  1672. file, as are netCDF records.  If the record addresses are random, the
  1673. efficiency of collection of a portion of the record data across
  1674. several or all records is reduced.  More importantly, the Clog
  1675. description of a file will fail in practice if the storage of an
  1676. exceedingly large number of exceedingly small records is attempted.
  1677. As a rule of thumb, you're in trouble if your record length is less
  1678. than the number of bytes in the "+record" statement necessary to
  1679. declare the record (this can't be less than 8 bytes).  If you are in
  1680. this category, you should strongly consider buffering several records
  1681. and writing them as an array for the sake of efficiency anyway.
  1682.  
  1683.  
  1684.  4I. Additional alignment information
  1685.  -----
  1686.  
  1687.      alignment_spec:
  1688.         "+" "align" alignment_type "[" alignment_value "]"
  1689.  
  1690.      alignment_type:
  1691.         "variables"
  1692.       | "structs"
  1693.  
  1694.    Some files, for example netCDF files, impose additional alignment
  1695. restrictions on variables.  This can be specified using the "+align
  1696. variables" syntax in Clog.  As a special case, alignment_value of 0
  1697. means that the same alignment should be applied to a variable in a
  1698. file as it were a member of a data structure.  An alignment_value of 1
  1699. means that there is no padding between variables; every variabe starts
  1700. on the byte immediately following the previous variable.  The
  1701. "@address" syntax in a variable declaration overrides the "+align
  1702. variables" statement.  The special value 0 is the Clog default; netCDF
  1703. files would have "+align variables 4", and PDB files would have "+align
  1704. variables 1".
  1705.  
  1706.    Some C compilers place an additional alignment restriction on
  1707. struct members which are themselves struct instances (beyond the usual
  1708. restriction that the alignment of a struct instance is the same as the
  1709. alignment of its most restrictively aligned member).  Such an
  1710. additional alignment restriction may be expressed in Clog via the
  1711. "+align structs" syntax.  The value 1 is the default, meaning that
  1712. there is no additional alignment restriction on struct instances.
  1713.  
  1714.    At most "+align" statement of each type is allowed in a Clog
  1715. description.  If present, these must come before any variables,
  1716. compound data structures, or records have been declared.
  1717.  
  1718.  
  1719.  4J. Predefined string and pointer formats
  1720.  -----
  1721.  
  1722.    As indicated above, the type_names "string" and "pointer" have an
  1723. optional predefined meaning, which is designed to map relatively
  1724. easily into the C language "char *" and "void *" data types.  In order
  1725. to do this, descriptive information unavoidably leaks from the
  1726. description of the binary data into the data itself.  The following
  1727. design minimizes this leakage; nevertheless, a binary file designer
  1728. should avoid gratuitous use of indirect data types.
  1729.  
  1730.    An instance of either string or pointer is represented on disk as a
  1731. long.  Hence, no use of the default string or pointer types may
  1732. precede a +define of the long primitive.  The value of that long is
  1733. interpreted as a disk address (as always, in bytes, with 0 meaning the
  1734. address of the first byte).  A disk address of -1 is taken as a NULL
  1735. pointer, meaning that there is no data associated with the pointer.
  1736.  
  1737.    In the case of a string, a character count of type long will be
  1738. found at the disk address specified in the (non-negative) pointer.
  1739. The characters of the string, if any, begin with the byte immediately
  1740. following the character count.  The terminating NULL character is not
  1741. included in either the character count or the string itself.
  1742.  
  1743.    In the case of a pointer, the (non-zero) pointer is the disk address
  1744. of a small header describing the pointee, followed by the pointee data
  1745. itself.  The header is an array of long integers encoded as follows:
  1746.  
  1747.      long          type_number     number representing the data type
  1748.                                    of the pointee data:
  1749.                                    0 char, 1 short, 2 int, 3 long,
  1750.                                    4 float, 5 double,
  1751.                                    6 string, 7 pointer,
  1752.                                    >=8 for the data types defined using
  1753.                                    +struct or +define in the order of
  1754.                                    definition in the Clog
  1755.      long          n_dims          number of dimensions (0 if scalar)
  1756.      long[n_dims]  length[n_dims]  (not present if n_dims==0)
  1757.                                    the number of elements along each
  1758.                                    dimension, in order from slowest
  1759.                                    varying to fastest varying dimension
  1760.      <garbage>     pad             any pad necessary to align the
  1761.                                    data to a disk address which would
  1762.                                    be acceptable if it were an ordinary
  1763.                                    variable
  1764.      <type_number> data            the pointee itself
  1765.  
  1766.  
  1767.  
  1768.  4K. Generic extension syntax
  1769.  -----
  1770.  
  1771.    The preceding sections cover the basic requirements for being able
  1772. to decipher the contents of a binary data file.  Of course, you can't
  1773. necessarily do anything with a bunch of numbers just because you can
  1774. read them.  In general, the meaning of the numbers in a binary file
  1775. emerges only from careful documentation.  The required level of
  1776. documentation is appropriate to a user's manual for the program which
  1777. wrote the file, not to the Clog description of the file.
  1778.  
  1779.    Nevertheless, sometimes it is appropriate to carry a higher level
  1780. of meaning around with a binary data file.  The Clog therefore provides
  1781. a generic syntax for such information:
  1782.  
  1783.      other_information:
  1784.         "+" public_extension [extension_id]
  1785.                              "{" extension_data "}" ["@" disk_address]
  1786.         "-" private_extension [extension_id]
  1787.                               "{" extension_data "}" ["@" disk_address]
  1788.  
  1789.      public_extension: identifier
  1790.  
  1791.      private_extension: identifier
  1792.  
  1793.      extension_id: identifier
  1794.  
  1795.      extension_data: <any sequence of tokens with balanced "{" and "}">
  1796.  
  1797. The notion of a public_extension is that considerable effort be
  1798. expended to ensure that the associated identifier be unique across all
  1799. implementations of the Clog.  A private_extension, on the other hand,
  1800. can be used immediately and freely at a single site.
  1801.  
  1802.    The private_extension syntax should not be used as a substitute for
  1803. the comment syntax /* ... */.
  1804.  
  1805.    A public_extension cannot have the identifiers "struct", "define",
  1806. "history", or "eod".  Furthermore, the following public_extensions are
  1807. hereby defined, in order to prevent the corresponding inevitable
  1808. private extensions:
  1809.  
  1810.  
  1811.      "+" "pedigree" "{" pedigree_spec ("," pedigree_spec)* "}"
  1812.  
  1813.      pedigree_spec:
  1814.         "created_by" "=" identifier
  1815.       | "creation_date" "=" identifier
  1816.       | "modified_by" "=" identifier
  1817.       | "modification_date" "=" identifier
  1818.       | "revision" "=" number
  1819.       | "archive_id" "=" identifier
  1820.       | "format_version" "=" number
  1821.       | identifier "=" identifier
  1822.       | identifier "=" number
  1823.  
  1824.    Blue bloods and bureaucrats demand pedigrees for their data.
  1825.  
  1826.  
  1827.      "+" "attributes" [variable_name]
  1828.                       "{" attribute_spec (";" attribute_spec)* "}"
  1829.  
  1830.      attribute_spec:
  1831.         attribute_name ["=" attribute_value]
  1832.  
  1833.      attribute_name: identifier
  1834.  
  1835.      attribute_value:
  1836.         number ("," number)*
  1837.       | float_number ("," float_number)*
  1838.       | identifier
  1839.  
  1840.    The attribute extension handles netCDF-style attributes.  The
  1841. number and float_number tokens are extended by the suffix notation
  1842. described in the netCDF User's Guide from Unidata, in the section on
  1843. CDL format.  An identifier as an attribute_value covers the case of a
  1844. string valued attribute; it should normally be a quoted string.  If
  1845. the variable_name is not present, the attributes apply to the whole
  1846. binary file.  If present, the variable_name must specify a previously
  1847. declared variable.
  1848.  
  1849.  
  1850.      "+" "value" variable_name "{" variable_value "}"
  1851.  
  1852.      variable_value:
  1853.         number ("," variable_value)*
  1854.       | float_number ("," variable_value)*
  1855.       | "{" variable_value "}"
  1856.  
  1857.    This extension is provided in order to be able to directly
  1858. translate Unidata CDL files into Clog files.  Just as the string and
  1859. pointer data types cause a leakage of descriptive information into the
  1860. data, the +value extension amounts to a leakage of data into the
  1861. descriptive information.  Each level of { ... } descends one level
  1862. into a structure instance.
  1863.  
  1864.  
  1865.      "+" "PDBpointer" ("variable" | "member") "{" type_name "}"
  1866.  
  1867.    The type_name must specify an opaque data type previously defined
  1868. by +define.  Two separate types should be supplied; one for variables,
  1869. which has size==0, and one for structure members, which has non-zero
  1870. size.  Both are sequential data types, since any object containing a
  1871. PDB-style pointer must be read sequentially as a complete block.  The
  1872. following declarations would be reasonable:
  1873.  
  1874.      +define "char *" [0][4][sequential]
  1875.      +PDBpointer variable { "char *" }
  1876.      +define "char  *" [4][4][sequential]  /* note extra space */
  1877.      +PDBpointer member { "char  *" }
  1878.  
  1879. These definitions assume that the sizeof(void *) specified in the PDB
  1880. prim_info section was 4, and the ptr_align specified in the PDB
  1881. Alignment extra block was 4.  There is no limit on the number of
  1882. different type_names which can be declared to be PDBpointer, but all
  1883. of the corresponding +define statements must be identical.
  1884.  
  1885.  
  1886.      "+" "PDBcast" type_name "{" member_pair ("," member_pair)* "}"
  1887.  
  1888.      member_pair: cast_member "," type_member
  1889.  
  1890.      cast_member: identifier
  1891.  
  1892.      type_member: identifier
  1893.  
  1894.    The PDBcast extension handles the information in the PDB Casts
  1895. extra block for PDB-style derived classes.  Given the clumsy nature of
  1896. the PDBpointer public_extension, this does not really work very
  1897. well...
  1898.